package com.useeinfo.oa.modules.inspection.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.inspection.enums.InsWorkingHoursStatusEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.persistence.*;

/**
 * @Auther: chenwei
 * @Date: 2019/1/24 13:16
 * @Description: 工时管理
 */
@Entity
@Table(name = "inspection_working_hours")
public class InspectionWorkingHours extends SuperClassEntity<InspectionWorkingHours> {
    /**
     * 是否已完成
     */
    @Column(columnDefinition = "bit default 0")
    private boolean completed;



    /**
     * 实际工时
     */
    private long actualWorkingHours;

    /**
     * 计划状态
     */
    @Enumerated(value = EnumType.STRING)
    @JoinColumn(name = "status")
    private InsWorkingHoursStatusEnum workingHoursStatusEnum;


    /**
     * 参与人 主参与人/辅助人
     */
    @ManyToOne
    @JoinColumn(name = "partner_id")
    private SystemUser partner;

    /**
     * 是否是主要负责人
     */
    @Column(columnDefinition = "bit default 0")
    private boolean isCharge;

    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 关联工单
     */
    @ManyToOne
    @JoinColumn(name = "inspection_order_id")
    private InspectionOrder inspectionOrder;

    public boolean isCompleted() {
        return completed;
    }

    public void setCompleted(boolean completed) {
        this.completed = completed;
    }

    public long getActualWorkingHours() {
        return actualWorkingHours;
    }

    public void setActualWorkingHours(long actualWorkingHours) {
        this.actualWorkingHours = actualWorkingHours;
    }

    public InsWorkingHoursStatusEnum getWorkingHoursStatusEnum() {
        return workingHoursStatusEnum;
    }

    public void setWorkingHoursStatusEnum(InsWorkingHoursStatusEnum workingHoursStatusEnum) {
        this.workingHoursStatusEnum = workingHoursStatusEnum;
    }

    public SystemUser getPartner() {
        return partner;
    }

    public void setPartner(SystemUser partner) {
        this.partner = partner;
    }

    public boolean isCharge() {
        return isCharge;
    }

    public void setCharge(boolean charge) {
        isCharge = charge;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public InspectionOrder getInspectionOrder() {
        return inspectionOrder;
    }

    public void setInspectionOrder(InspectionOrder inspectionOrder) {
        this.inspectionOrder = inspectionOrder;
    }

    @Override
    public JSONObject toJSONObject(){
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("completed", isCompleted()? "是" : "否");
        jsonObject.put("orderName",StringConverters.null2String(getInspectionOrder().getOrderName()));
        jsonObject.put("orderCode",StringConverters.null2String(getInspectionOrder().getOrderCode()));
        jsonObject.put("startDate",getInspectionOrder().getStartDateTime() == null ? "" : DateTimeUtils.format(getInspectionOrder().getStartDateTime(),"yyyy-MM-dd"));
        jsonObject.put("endDate",getInspectionOrder().getEndDateTime() == null ? "" : DateTimeUtils.format(getInspectionOrder().getEndDateTime(),"yyyy-MM-dd"));

        jsonObject.put("startTime",getInspectionOrder().getStartDateTime() == null ? "" : DateTimeUtils.format(getInspectionOrder().getStartDateTime(),"HH:mm:ss"));
        jsonObject.put("endTime",getInspectionOrder().getEndDateTime() == null ? "" : DateTimeUtils.format(getInspectionOrder().getEndDateTime(),"HH:mm:ss"));
        jsonObject.put("assign",getInspectionOrder().getAssign() ? "是" : "否");
        jsonObject.put("receiveUserName",getInspectionOrder().getReceiveUser() == null ? "":getInspectionOrder().getReceiveUser().getUserName());
        jsonObject.put("presence",getInspectionOrder().getPresenceTime() != null ? "是" : "否");
        jsonObject.put("receive",getInspectionOrder().getReceiveUser() != null ? "是" : "否");
        jsonObject.put("latitude",StringConverters.null2Long(getInspectionOrder().getLatitude()));
        jsonObject.put("longitude",StringConverters.null2Long(getInspectionOrder().getLongitude()));
        jsonObject.put("status",getWorkingHoursStatusEnum().getName());
        jsonObject.put("userName",getPartner().getUserName());
        jsonObject.put("workingHours",getActualWorkingHours());
        jsonObject.put("orderType",getInspectionOrder().isOrderType() ? "一次性" : "周期性");
        jsonObject.put("enterpriseName",StringConverters.null2String(getEnterprise().getEnterpriseName()));
        return jsonObject;
    }
}
